/*
 *    Copyright 2009 Simone Tripodi
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.memos2;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import net.spy.memcached.DefaultConnectionFactory;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.opensymphony.oscache.base.Config;

/**
 * @author Simone Tripodi (simone.tripodi)
 * @version $Id: MemcachedConfigurationBuilderTestCase.java 94 2009-12-16 09:46:27Z simone.tripodi $
 */
public final class MemcachedConfigurationBuilderTestCase {

    @DataProvider
    @SuppressWarnings("serial")
    public static Object[][] configurations() {
        // testcase 1 - empty config and default memcached config
        Config config1 = new Config();

        MemcachedConfiguration expected1 = new MemcachedConfiguration();
        expected1.setAddresses(new ArrayList<InetSocketAddress>(){{add(new InetSocketAddress("localhost", 11211));}});
        expected1.setCompressionEnabled(false);
        expected1.setConnectionFactory(new DefaultConnectionFactory());
        expected1.setEncryptKeys(false);
        expected1.setExpiration(60 * 60 * 24 * 30);
        expected1.setGroupPrefix("__group__");
        expected1.setKeyPrefix("_oscache_");
        expected1.setTimeout(5);
        expected1.setTimeUnit(TimeUnit.SECONDS);
        expected1.setUsingAsyncGet(false);

        // testcase 2 - wrong input config will be ignored
        Config config2 = new Config();
        config2.set("cache.memos2.servers", "");
        config2.set("cache.memos2.connectionfactory", "com.acme.FooConnectionFactory");
        config2.set("cache.memos2.encryptkeys", "yes");
        config2.set("cache.memos2.timeoutunit", "years");

        return new Object[][]{
                { config1, expected1 },
                { config2, expected1 }
        };
    }

    @Test(
            dataProvider = "configurations",
            dataProviderClass = MemcachedConfigurationBuilderTestCase.class
    )
    public void confrontingConfigurations(Config config, MemcachedConfiguration expected) {
        MemcachedConfiguration actual = MemcachedConfigurationBuilder.getInstance().parseConfiguration(config);

        assert expected.getAddresses().equals(actual.getAddresses());
        assert expected.isCompressionEnabled() == actual.isCompressionEnabled();
        assert expected.getConnectionFactory().getClass() == actual.getConnectionFactory().getClass();
        assert expected.isEncryptKeys() == actual.isEncryptKeys();
        assert expected.getExpiration() == actual.getExpiration();
        assert expected.getGroupPrefix().equals(actual.getGroupPrefix());
        assert expected.getKeyPrefix().equals(actual.getKeyPrefix());
        assert expected.getTimeout() == actual.getTimeout();
        assert expected.getTimeUnit().equals(actual.getTimeUnit());
        assert expected.isUsingAsyncGet() == actual.isUsingAsyncGet();
    }

}
